﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using MyCommerce.Presentation.GeneralParameters.IViews;
using Innova.Microfinanzas.BusinessEntities;
using MyCommerce.Business.GeneralParameters;
using Innova.Microfinanzas.BusinessEntities.EnumClassifierTypes;

namespace MyCommerce.Presentation.GeneralParameters.Presenters
{
    public class CurrencyExchangePresenter
    {
        private ICurrencyExchangeView pViewToPresent;

        public CurrencyExchangePresenter(ICurrencyExchangeView vView)
        {
            pViewToPresent = vView;
            pViewToPresent = vView;
            pViewToPresent.OnLoad += new EventHandler(vManagedView_OnLoad);
            pViewToPresent.OnDelete += new IViews.OnEntityEventHandler(vManagedView_OnDelete);
            pViewToPresent.OnSave += new IViews.OnSaveEventHandler(vManagedView_OnSave);

        }

        private void LoadExchangeRatesList()
        {
            CurrencyExchangeRatesManager vCommonClassifierManager = new CurrencyExchangeRatesManager();
            pViewToPresent.ColExchangeRates = vCommonClassifierManager.GetExchangeRatesList();
        }

        void vManagedView_OnSave(object sender, bool isNew)
        {
            CurrencyExchangeRatesManager vCommonClassifierManager = new CurrencyExchangeRatesManager();
            if (isNew)
            {
                vCommonClassifierManager.SaveExchangeRate(pViewToPresent.objExchangeRate);
            }
            else
            {
                //vCommonClassifierManager.UpdateClassifier(pViewToPresent.objExchangeRate);
            }
            LoadExchangeRatesList();
        }

        void vManagedView_OnDelete(object sender, long id)
        {
            CurrencyExchangeRatesManager exchangeMgr = new CurrencyExchangeRatesManager(true);
            exchangeMgr.DeleteExchangeRate(id);
            LoadExchangeRatesList();
        }

        void vManagedView_OnLoad(object sender, EventArgs e)
        {
            CommonClassifierManager commonMgr = new CommonClassifierManager();
            pViewToPresent.colMoneys = commonMgr.GetClassifierByIDType(Convert.ToInt64(CommonClassifierTypes.CurrencyClassfier));
            LoadExchangeRatesList();
        }
    }
}
